﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using com.lover.astd.common.config;
using com.lover.astd.common.manager;
using com.lover.astd.common.model.enumer;

namespace com.lover.astd.common.model
{
    public class AccountData
    {
        #region inner fields
        private int _id;
        /// <summary>
        /// 账户ID
        /// </summary>
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private ServerType _serverType;
        /// <summary>
        /// 服务器类型
        /// </summary>
        public ServerType Server_type
        {
            get { return _serverType; }
            set { _serverType = value; }
        }
        private int _serverId;
        /// <summary>
        /// 几区
        /// </summary>
        public int ServerId
        {
            get { return _serverId; }
            set { _serverId = value; }
        }
        public string getServerName() { return String.Format("{0}_{1}区", EnumString.getString(Server_type), _serverId); }
        private string _userName;
        /// <summary>
        /// 登录用户名
        /// </summary>
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }
        private string password;
        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        private string _roleName;
        /// <summary>
        /// 角色名
        /// </summary>
        public string RoleName
        {
            get { return _roleName; }
            set { _roleName = value; }
        }
        private string _customLoginUrl;
        /// <summary>
        /// 自定义的登录地址
        /// </summary>
        public string CustomLoginUrl
        {
            get { return _customLoginUrl; }
            set { _customLoginUrl = value; }
        }
        private string _customGameUrl;
        /// <summary>
        /// 自定义的游戏地址
        /// </summary>
        public string CustomGameUrl
        {
            get { return _customGameUrl; }
            set { _customGameUrl = value; }
        }
        private bool _isLastLogin;
        /// <summary>
        /// 是否是最后登录
        /// </summary>
        public bool IsLastLogin
        {
            get { return _isLastLogin; }
            set { _isLastLogin = value; }
        }
        private AccountStatus _status = AccountStatus.NotStart;
        /// <summary>
        /// 账号状态
        /// </summary>
        public AccountStatus Status
        {
            get { return _status; }
            set { _status = value; }
        }
        private GameConfig _gameConf;
        /// <summary>
        /// 游戏配置
        /// </summary>
        public GameConfig GameConf
        {
            get { return _gameConf; }
            set { _gameConf = value; }
        }
        private string _gameUrl;
        /// <summary>
        /// 游戏地址
        /// </summary>
        public string GameUrl
        {
            get { return _gameUrl; }
            set {
                _gameUrl = value; 
            }
        }
        private string _jsessionId;
        /// <summary>
        /// 当前的session串
        /// </summary>
        public string JsessionId
        {
            get { return _jsessionId; }
            set { _jsessionId = value; }
        }
        private List<Cookie> _cookies = new List<Cookie>();
        /// <summary>
        /// 当前的cookie
        /// </summary>
        public List<Cookie> Cookies
        {
            get { return _cookies; }
            set { _cookies = value; }
        }

        public string Desc() 
        {
            if (_serverType == ServerType.Custom)
            {
                return String.Format("[{0}] {1}",
                    EnumString.getString(_status),
                    EnumString.getString(_serverType));
            }
            else
            {
                return String.Format("[{0}] {1}({2}区):{3}{4}",
                    EnumString.getString(_status),
                    EnumString.getString(_serverType),
                    _serverId.ToString(),
                    _userName,
                    _roleName == "" ? "" : "|" + _roleName);
            }
        }
        public override string ToString()
        {
            return _userName;
        }
        public void setServerType(string server_typestr)
        {
            foreach (ServerType tp in Enum.GetValues(typeof(ServerType)))
            {
                if (server_typestr.Equals(tp.ToString()))
                {
                    _serverType = tp;
                    break;
                }
            }
        }
        public override bool Equals(object accdt)
        {
            if (accdt is AccountData)
            {
                AccountData acc = accdt as AccountData;
                if (this.Server_type == ServerType.Custom) return acc.Server_type == ServerType.Custom;
                else return this.Server_type == acc.Server_type && this.UserName == acc.UserName
                        && this.ServerId == acc.ServerId && this.RoleName == acc.RoleName;
            }
            return false;
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        #endregion
        public string generateGameId()
        {
            return String.Format("{0}_{1}_{2}_{3}", EnumString.getString(_serverType), _serverId, _userName, _roleName);
        }
    }
}
